package com.fast.admin.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.fast.admin.common.utils.MenuTreeUtil;
import com.fast.admin.entity.sys.Resource;
import com.fast.admin.entity.sys.RoleResource;
import com.fast.admin.mapper.sys.IResourceMapper;
import com.fast.admin.service.sys.IResourceService;
import com.fast.admin.service.sys.IRoleResourceService;
import com.fast.admin.vo.LayXtreeVo;
import com.fast.admin.vo.ResourceTreeVo;
import com.fast.admin.vo.SelectTreeVo;
import com.fast.common.annotation.Commit;
import com.fast.common.base.service.impl.BaseService;
import com.fast.common.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title：资源服务
 * @author：ylc
 * @date：2018/7/20 15:52
 * @version：1.0
 **/
@Service
public class ResourceService extends BaseService<IResourceMapper, Resource> implements IResourceService {

    @Autowired
    private IRoleResourceService roleResourceService;

    @Override
    @Commit
    public void addOrUpdate(Resource resource) {
        if (CommonUtil.properIsNULL(resource.getId())) {
            //检查是否存在相同name的数据
            Resource temp = selectByName(resource.getName());
            if (CommonUtil.isEmpty(temp)) {
                //TODO 暂时默认是添加菜单
                resource.setType(0);
                resource.setStatus(1);
                super.insert(resource);
            } else {
                throw new ApiException("["+resource.getName() + "]" + "数据已存在相同的");
            }
        } else {
            super.updateById(resource);
        }
    }

    @Override
    public List<SelectTreeVo> findSelectMenuTree() {
        List<Resource> resources = super.selectList(null);
        List<SelectTreeVo> selectTreeVos = resources.stream().map(c -> new SelectTreeVo(c.getId(), CommonUtil.properIsNULL(c.getPid())?0:c.getPid(), c.getName())).collect(Collectors.toList());
        return selectTreeVos;
    }

    @Override
    public List<ResourceTreeVo> findResourceTree() {
        List<Resource> resources = super.selectList(null);
        List<ResourceTreeVo> resourceTreeVos = resources.stream().map(c -> new ResourceTreeVo(c.getId(), c.getPid(), c.getName(), c.getType(), null, c.getUrl())).collect(Collectors.toList());
        return MenuTreeUtil.buildTreeNode(resourceTreeVos);
    }

    @Override
    public List<LayXtreeVo> getLayXtreeByRole(Long roleId) {
        List<Resource> resources = super.selectList(null);
        List<LayXtreeVo> layXtreeVoList = resources.stream().map(c -> new LayXtreeVo(String.valueOf(c.getId()), c.getName(), c.getPid())).collect(Collectors.toList());
        List<RoleResource> roleResources = roleResourceService.selectByRole(roleId);
        if (!roleResources.isEmpty()){
            for (LayXtreeVo layXtreeVo : layXtreeVoList) {
                for (RoleResource roleResource : roleResources) {
                    if (layXtreeVo.getValue().equals(String.valueOf(roleResource.getResourceId()))){
                        layXtreeVo.setChecked(true);
                        break;
                    }
                }
            }
        }
        List<LayXtreeVo> layXtreeVos = MenuTreeUtil.buildLayXtreeNode(layXtreeVoList);
        return layXtreeVos;
    }


    private Resource selectByName(String name) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("name", name);
        return super.selectOne(wrapper);
    }

}
